<?php

/**
 * This is the model class for table "tb_peserta".
 *
 * The followings are the available columns in table 'tb_peserta':
 * @property integer $id
 * @property integer $tb_kelas_id
 * @property string $nama
 * @property string $jeniskelamin
 * @property string $tempatlahir
 * @property string $tanggallahir
 * @property string $alamat
 * @property string $telepon
 * @property string $pendidikanterakhir
 * @property string $foto
 * @property integer $tahunajaran
 * @property string $tanggaldaftar
 * @property string $tanggalmulai
 * @property string $tanggalselesai
 * @property string $password
 *
 * The followings are the available model relations:
 * @property Absensi[] $absensis
 * @property Kelas $tbKelas
 */
class Peserta extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_peserta';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, tb_kelas_id, nama, jenis_kelamin, tempat_lahir, tanggal_lahir, alamat, telepon, pendidikan_terakhir, foto, tahun_ajaran, tanggal_daftar, tanggal_mulai, tanggal_selesai, password', 'required'),
			array('id, tb_kelas_id, tahun_ajaran', 'numerical', 'integerOnly'=>true),
			array('nama, foto, password', 'length', 'max'=>100),
			array('jenis_kelamin', 'length', 'max'=>1),
			array('tempat_lahir, pendidikan_terakhir', 'length', 'max'=>20),
			array('telepon', 'length', 'max'=>15),
			/*
			//Example username
			array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u',
                 'message'=>'Username can contain only alphanumeric 
                             characters and hyphens(-).'),
          	array('username','unique'),
          	*/
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, tb_kelas_id, nama, jenis_kelamin, tempat_lahir, tanggal_lahir, alamat, telepon, pendidikan_terakhir, foto, tahun_ajaran, tanggal_daftar, tanggal_mulai, tanggal_selesai, password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'absensis' => array(self::HAS_MANY, 'Absensi', 'tb_peserta_id'),
			'Kelas' => array(self::BELONGS_TO, 'Kelas', 'tb_kelas_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tb_kelas_id' => 'Kelas',
			'nama' => 'Nama',
			'jenis_kelamin' => 'Jenis Kelamin',
			'tempat_lahir' => 'Tempat Lahir',
			'tanggal_lahir' => 'Tanggal Lahir',
			'alamat' => 'Alamat',
			'telepon' => 'Telepon',
			'pendidikan_terakhir' => 'Pendidikan Terakhir',
			'foto' => 'Foto',
			'tahun_ajaran' => 'Tahun Ajaran',
			'tanggal_daftar' => 'Tanggal Daftar',
			'tanggal_mulai' => 'Tanggal Mulai',
			'tanggal_selesai' => 'Tanggal Selesai',
			'password' => 'Password',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tb_kelas_id',$this->tb_kelas_id);
		$criteria->compare('nama',$this->nama,true);
		$criteria->compare('jenis_kelamin',$this->jenis_kelamin,true);
		$criteria->compare('tempat_lahir',$this->tempat_lahir,true);
		$criteria->compare('tanggal_lahir',$this->tanggal_lahir,true);
		$criteria->compare('alamat',$this->alamat,true);
		$criteria->compare('telepon',$this->telepon,true);
		$criteria->compare('pendidikan_terakhir',$this->pendidikan_terakhir,true);
		$criteria->compare('foto',$this->foto,true);
		$criteria->compare('tahun_ajaran',$this->tahun_ajaran);
		$criteria->compare('tanggal_daftar',$this->tanggal_daftar,true);
		$criteria->compare('tanggal_mulai',$this->tanggal_mulai,true);
		$criteria->compare('tanggal_selesai',$this->tanggal_selesai,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Peserta the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function beforeSave() 
    {
        $userId=0;
		if(null!=Yii::app()->user->id) $userId=(int)Yii::app()->user->id;
		
		if($this->isNewRecord)
        {           
                        						$this->password = $this->hashPassword($this->password);
			
			
        }else{
                        						if(!empty($this->password)){
	        	$this->password = $this->hashPassword($this->password);
	    	}
			
			
        }

        
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('d/m/Y',$this->tanggallahir);
        	//$this->tanggallahir=$from->format('Y-m-d');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('d/m/Y',$this->tanggaldaftar);
        	//$this->tanggaldaftar=$from->format('Y-m-d');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('d/m/Y',$this->tanggalmulai);
        	//$this->tanggalmulai=$from->format('Y-m-d');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('d/m/Y',$this->tanggalselesai);
        	//$this->tanggalselesai=$from->format('Y-m-d');
        	
        return parent::beforeSave();
    }

    public function beforeDelete () {
		$userId=0;
		if(null!=Yii::app()->user->id) $userId=(int)Yii::app()->user->id;
                                
        return false;
    }

    public function afterFind()    {
         
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('Y-m-d',$this->tanggallahir);
        	//$this->tanggallahir=$from->format('d/m/Y');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('Y-m-d',$this->tanggaldaftar);
        	//$this->tanggaldaftar=$from->format('d/m/Y');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('Y-m-d',$this->tanggalmulai);
        	//$this->tanggalmulai=$from->format('d/m/Y');
        	
        	// NOT SURE RUN PLEASE HELP ME -> 
        	//$from=DateTime::createFromFormat('Y-m-d',$this->tanggalselesai);
        	//$this->tanggalselesai=$from->format('d/m/Y');
        	
        parent::afterFind();
    }
	
		
	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return CPasswordHelper::verifyPassword($password,$this->password);
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @return string hash
	 */
	public static function hashPassword($password)
	{
		return CPasswordHelper::hashPassword($password);
	}
	
	
	public function defaultScope()
    {
    	/*
    	//Example Scope
    	return array(
	        'condition'=>"deleted IS NULL ",
            'order'=>'create_time DESC',
            'limit'=>5,
        );
        */
        $scope=array();

        
        return $scope;
    }
}
